//////EzProjectSelector
// timePicker

(function ($, undefined) {

    // define the name of the UI component
    var ui_name = "projectSelector"

    // initialize object
    var ps = $.projectSelector = {

        // default values
        defaults: {

    },

    setDefaults: function (defaultOptions) {
        this.defaults = $.extend({}, this.defaults, defaultOptions);
    }
};

$.fn.projectSelector = function (options) {
    var _defaultProject = '无';
    var _defaultPlane = '无';

    var _projectInitList = null;
    var _planeInitList = null;

    var _projectId = null;
    var __planeId = null;

    var _projectLabel = null;
    var _planeLabel = null;

    var _projectSelector = null;
    var _planeSelector = null;

    var _domInitialized = false;


    // method calling
    if (typeof options == 'string') {
        var args = Array.prototype.slice.call(arguments, 1),
			res;
        this.each(function () {
            var data = $.data(this, ui_name);
            if (data) {
                var meth = data[options];
                if (meth) {
                    var r = meth.apply(this, args);
                    if (res === undefined) {
                        res = r;
                    }
                }
            }
        });
        if (res !== undefined) {
            return res;
        }
        return this;
    }

    // initialize options
    options = $.extend({}, ps.defaults, options);

    this.each(function () {
        var _self = this;
        if (_self) {
            var dom = getInitializeDom();
            $(this).append(dom);
        }


        // initialize callback
        for (var key in options) {
            var value = options[key];
            if (typeof value == 'function') {
                // bind callbacks
                $(_self).bind(key, value);
            }
        }

        /* private Methods
        -----------------------------------------------------------------------------*/
        function _accessor() {

            var _self = this;
            var accessor = {};

            if (ezmock.IS_MOCKED) {

                var mock_planeList = [
                                        { 'Id': '1',
                                            'Name': '调研阶段'
                                        },
                                        { 'Id': '2',
                                            'Name': '实地勘察'
                                        },
                                        { 'Id': '3',
                                            'Name': '规划设计'
                                        },
                                        { 'Id': '4',
                                            'Name': '设计交付'
                                        }];


                var mock_projectList = [
                                        { 'Id': '1',
                                            'Name': '三亚景观'
                                        },
                                        { 'Id': '2',
                                            'Name': '二亚景观'
                                        }
                                       ];

                var mockAccssor = {

                    'getProjectList': function () {
                        _projectInitList = mock_projectList;
                    },

                    'doUpdatePlaneList': function () {
                        _planeInitList = mock_planeList;
                    }

                };

                accessor = mockAccssor;

            } else {

                var realAccessor = {

                    'getProjectList': function () {

                        
                    },

                    'doUpdatePlaneList': function () {

                       
                    }

                };

                accessor = realAccessor;
            }

            return accessor;

        };


        function _getInitProjectList() {

            var project = _projectInitList;
            var initProjectList = [];

            for (var key in project) {
                var name = project[key].Name;
                var id = project[key].Id;

                initProjectList.push({ 'display': name,
                    'value': id
                });
            }
            return initProjectList;
        };



        function _bindProjectList() {
            var projectDropDownList = _getInitProjectList();

            _projectSelector
                     .comboxBindingWithEmptyDefault(
                                            projectDropDownList,
                                            'display',
                                            'value',
                                            _defaultProject);
        };



        function _getInitPlaneList() {
            var initPlaneList = [];

            for (var key in _planeInitList) {
                var name = _planeInitList[key].Name;
                var id = _planeInitList[key].Id;

                initPlaneList.push({ 'display': name,
                    'value': id
                });
            }

            return initPlaneList;

        };


        function _bindPlaneList() {

            var planeDropDownList = _getInitPlaneList();

            _planeSelector
                     .comboxBindingWithEmptyDefault(
                                        planeDropDownList,
                                        'display',
                                        'value',
                                        _defaultPlane);

        };


        function _projectChangedCallback(id) {
                if (id == _defaultProject) {
                    _planeLabel.hide()
                               .next('select').hide();

                } else {
                    _accessor().doUpdatePlaneList();
                    _bindPlaneList();

                    _planeLabel.show()
                               .next('select').show();
                }



        
        };

        
        function _getCallbackjson() {
            var callbackJson = {};
                callbackJson.ProjectId = _projectId;
                callbackJson.PlaneId = _planeId;

            return callbackJson;
        };


        function getInitializeDom() {
            var domRoot = $("<div />")
            var wrapProjectSelector = $("<div class='l' style='float: left;' />");
            var wrapPlaneSelector = $("<div class='r' style='float: left;' />");

            _projectLabel = $("<span id='lblProject'>选择项目：</span>");
            _projectSelector = $("<select id='sltProject'/>");

            _planeLabel = $("<span id='lblPlane' style='display: none;'>&nbsp;&nbsp;&nbsp;选择阶段：</span>");
            _planeSelector = $("<select id='sltPlane' style='display: none;' />");

            wrapProjectSelector.append(_projectLabel);
            wrapProjectSelector.append(_projectSelector);

            wrapPlaneSelector.append(_planeLabel);
            wrapPlaneSelector.append(_planeSelector);

            domRoot.append(wrapProjectSelector);
            domRoot.append(wrapPlaneSelector);

            _accessor().getProjectList();
            _bindProjectList();
            
            //todo.....
            _accessor().doUpdatePlaneList(_projectSelector.val());
            _bindPlaneList();
            _planeLabel.show()
                       .next('select').show();

            //projectSelector changedCallback
            _projectSelector.change(function(){
                _projectId = _projectSelector.val();
                _planeId = _planeSelector.val();

                var callbackJson = _getCallbackjson();
                //获得项目阶段列表
                _projectChangedCallback(_projectId);
                //项目改变callback
                onSelect(callbackJson);
            });


            //_planeSelector changedCallback
            _planeSelector.change(function(){
                _projectId = _projectSelector.val();
                _planeId = _planeSelector.val();

                var callbackJson = _getCallbackjson();
                //阶段改变callback
                onSelect(callbackJson);
            });




            //                _projectSelector.change(function () {
            //                    selectProjectChangedCallBack();
            //                });

            //                _domInitialized = true;

            return domRoot;

        };

        /* Callback Triggers
        -----------------------------------------------------------------------------*/

        function onSelect(id) {
            $(_self).trigger('onSelect', id);
        };





        /* Public Methods
        -----------------------------------------------------------------------------*/



        /* Public Methods
        -----------------------------------------------------------------------------*/

        var publicMethods = {
            'option': function (name, value) {
                if (value === undefined) {
                    return options[name];
                }
                options[name] = value;
            },


            //设置当前项目
            'setProject': function (id) {
                _projectId = id;

                //处理异步通信
                for (var key in _projectInitList) {
                    if(id == _projectInitList[key].Id) {
                        _projectSelector.val(_projectId);
                    }
                }
               
            },


            //获得所选项目
            'getProject': function () {
                var selectedProject = {};

                selectedProject.Id = _projectSelector.val();
                selectedProject.Name = _projectSelector.find("option:selected").text();

                return selectedProject;
            },

            //获得所选项目阶段
            'getPlane': function () {   
                var selectedPlane = {};

                selectedPlane.Id = _planeSelector.val();
                selectedPlane.Name = _planeSelector.find("option:selected").text();

                return selectedPlane;
            },

            // @other public methods
        };

        $.data(this, ui_name, publicMethods);


        // define callback triggers

        //function select() {
        //    var val = 12345;
        //    $(_self).trigger('select', val);
        //}

    });

    return this;
};

// @static methods

})(jQuery);
//var EzProjectSelector = function (selectorId) {
//    this._selectorId = selectorId;
//    this._init();
//};

//EzProjectSelector.prototype = {

//    '_init': function () {

//    },

//    '_accessor': function () {

//        var _self = this;
//        var accessor = {};

//        if (ezmock.IS_MOCKED) {

//            var mock_planeList = [
//                                    { 'Id': '1',
//                                        'Name': '调研阶段'
//                                    },
//                                    { 'Id': '2',
//                                        'Name': '实地勘察'
//                                    },
//                                    { 'Id': '3',
//                                        'Name': '规划设计'
//                                    },
//                                    { 'Id': '4',
//                                        'Name': '设计交付'
//                                    }];

//            var mockAccssor = {

//                'getProjectList': function () {
//                    return mock_projectList;
//                },

//                'doUpdatePlaneList': function () {
//                    _self._planeInitList = _self._getInitPlaneList(mock_planeList);
//                }

//            };

//            accessor = mockAccssor;

//        } else {

//            var realAccessor = {

//                'getProjectList': function () {

//                    return _self._projectList;
//                },

//                'doUpdatePlaneList': function () {

//                    _self.setPlane();
//                }

//            };

//            accessor = realAccessor;
//        }

//        return accessor;

//    },

//    'selectProjectChangedCallBack': function () {

//        var _self = this;
//        _self.projectChanged();
//    },

//    '_defaultProject': '无',
//    '_defaultPlane': '无',
//    '_projectList': null,
//    '_projectInitList': null,
//    '_planeURL': null,
//    '_planeList': null,
//    '_planeInitList': null,
//    '_selectorId': null,
//    '_projectLabel': null,
//    '_planeLabel': null,
//    '_projectSelector': null,
//    '_planeSelector': null,
//    '_selectedProject': null,
//    '_selectedPlane': null,
//    '_domInitialized': false,

//    '_getInitProjectList': function ( ) {

//        var _self = this;
//        var project = _self._projectList;
//        var initProjectList = [];

//        for (var key in project) {
//            var name = project[key].Name;
//            var id = project[key].Id;

//            initProjectList.push({ 'display': name,
//                'value': id
//            });
//        }
//        return initProjectList;
//    },

//    '_bindProjectList': function ( ) {

//        var _self = this;
//        var projectDropDownList = _self._projectInitList;

//        _self._projectSelector
//                 .comboxBindingWithEmptyDefault(
//                                    projectDropDownList,
//                                    'display',
//                                    'value',
//                                    _self._defaultProject);
//    },

//    '_getInitPlaneList': function (planeList) {

//        var _self = this;
//        var initPlaneList = [];

//        for (var key in planeList) {
//            var name = planeList[key].Name;
//            var id = planeList[key].Id;

//            initPlaneList.push({ 'display': name,
//                'value': id
//            });
//        }

//        return initPlaneList;

//    },

//    '_bindPlaneList': function () {

//        var _self = this;
//        var planeDropDownList = _self._planeInitList;

//        _self._planeSelector
//                 .comboxBindingWithEmptyDefault(
//                                    planeDropDownList,
//                                    'display',
//                                    'value',
//                                    _self._defaultPlane);

//        _self._planeLabel
//                    .hide()
//                    .next('select').hide();
//    },

//    'getProject': function () {

//        var _self = this;
//        var selectedProject = {};

//        selectedProject.Id = _self._projectSelector.val();
//        selectedProject.Name = _self._projectSelector.find("option:selected").text();

//        return selectedProject;
//    },

//    'setProject': function (projectList) {

//        var _self = this;

//        _self._projectList = projectList;

//    },

//    'setPlaneURL': function (URL) {

//        var _self = this;
//        _self._planeURL = URL;
//    },

//    'getPlane': function () {

//        var _self = this;
//        var selectedPlane = {};

//        selectedPlane.Id = _self._planeSelector.val();
//        selectedPlane.Name = _self._planeSelector.find("option:selected").text();

//        return selectedPlane;
//    },

//    'setPlane': function () {

//        var _self = this;
//        var projectSelected = _self._projectSelector.val();
//        var PLANEURL = _self._planeURL;

//        $.ajaxBusy('begin');
//        $.getJSON(
//                    PLANEURL,
//                    { 'projectId': projectSelected },
//                    function (e) {

//                        $.ajaxBusy('end');
//                        _self._planeInitList = _self._getInitPlaneList(e);
//                    });

//    },

//    'projectChanged': function () {

//        var _self = this;

//        if (_self._projectSelector.val() == _self._defaultProject) {

//            _self._planeLabel
//                        .hide()
//                        .next('select').hide();
//        } else {

//            _self._accessor().doUpdatePlaneList();
//            _self._bindPlaneList();
//            _self._planeLabel
//                        .show()
//                        .next('select').show();

//        }
//    },

//    'getInitializeDom': function () {

//        var _self = this;

//        var domRoot = $("<div />")
//        var wrapProjectSelector = $("<div class='l' style='float: left;' />");
//        var wrapPlaneSelector = $("<div class='r' style='float: left;' />");

//        _self._projectLabel = $("<span id='lblProject'>选择项目：</span>");
//        _self._projectSelector = $("<select id='sltProject'/>");

//        _self._planeLabel = $("<span id='lblPlane' style='display: none;'>&nbsp;&nbsp;&nbsp;选择阶段：</span>");
//        _self._planeSelector = $("<select id='sltPlane' style='display: none;' />");

//        wrapProjectSelector.append(_self._projectLabel);
//        wrapProjectSelector.append(_self._projectSelector);
//        domRoot.append(wrapProjectSelector);

//        wrapPlaneSelector.append(_self._planeLabel);
//        wrapPlaneSelector.append(_self._planeSelector);
//        domRoot.append(wrapPlaneSelector);

//        _self._projectInitList = _self._getInitProjectList(_self._projectList);
//        _self._bindProjectList();

//        _self._projectSelector.change(function () {
//            _self.selectProjectChangedCallBack();
//        });

//        _self._domInitialized = true;

//        return domRoot;
//    }
//};

//  $.fn.projectSelector = function (projectSelector) {
//    $(this).each(function (index, item) {
//        if (this) {
//            var dom = projectSelector.getInitializeDom();
//            $(this).append(dom);
//        }
//    });
//};